package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.mapper.LitemallAdDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallAdService {
    @Resource
    private LitemallAdDao adDao;

    public List<LitemallAd> queryIndex() {
    	LitemallAd ad = new LitemallAd();
    	
    	ad.setPosition((byte) 1);
    	ad.setDeleted(false);
    	ad.setEnabled(true);
    	return adDao.template(ad);
		/*
		 * LitemallAdExample example = new LitemallAdExample();
		 * example.or().andPositionEqualTo((byte)
		 * 1).andDeletedEqualTo(false).andEnabledEqualTo(true); return
		 * adMapper.selectByExample(example);
		 */
    }

    public List<LitemallAd> querySelective(String name, String content, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallAd> query = adDao.createLambdaQuery();
    	 if (!StringUtils.isEmpty(name)) {
    		 query.andLike(LitemallAd::getName, "%" + name + "%");
         }
         if (!StringUtils.isEmpty(content)) {
        	 query.andLike(LitemallAd::getContent, "%" + content + "%");
         }
         query.andEq(LitemallAd::getDeleted, false);
         
         query.limit(page, limit);

         if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	 query.orderBy(sort + " " + order);
         }
         List<LitemallAd> list = query.select();
         return list;
    	/*
    	
        LitemallAdExample example = new LitemallAdExample();
        LitemallAdExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return adMapper.selectByExample(example);*/
    }

    public int updateById(LitemallAd ad) {
    	
        ad.setUpdateTime(LocalDateTime.now());
        return adDao.updateTemplateById(ad);
		/* return adMapper.updateByPrimaryKeySelective(ad); */
    }

    public void deleteById(Integer id) {
    	LitemallAd ad = new LitemallAd();
    	ad.setId(id);
    	ad.setDeleted(true);
    	adDao.updateTemplateById(ad);
		/* adMapper.logicalDeleteByPrimaryKey(id); */
    }

    public void add(LitemallAd ad) {
        ad.setAddTime(LocalDateTime.now());
        ad.setUpdateTime(LocalDateTime.now());
        adDao.insertTemplate(ad,true);
		/* adMapper.insertSelective(ad); */
    }

    public LitemallAd findById(Integer id) {
    	return adDao.single(id);
		/* return adMapper.selectByPrimaryKey(id); */
    }
}
